//
//  InvitationViewController.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/4/27.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "InvitationViewController.h"
#import "AppContext.h"
#import "SDWebImageManager.h"
#import "WXApi.h"
@interface InvitationViewController ()

@property (weak, nonatomic) IBOutlet UILabel *invitationLabel;
@property (weak, nonatomic) IBOutlet UIView *inviteView;
@end

@implementation InvitationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.invitationLabel.text = [NSString stringWithFormat:@"%@", APP_CONTEXT.account.data.invitation_code];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closePageAction:)]];

    // Do any additional setup after loading the view from its nib.
}

- (IBAction)closePageAction:(id)sender {
    if ([sender isKindOfClass:[UITapGestureRecognizer class]]) {
        UITapGestureRecognizer *gesture = sender;
        CGPoint location = [gesture locationInView:self.view];
        if (CGRectContainsPoint(self.inviteView.frame, location)) {
            return;
        }
    }
    if (self.closePageBlock) {
        self.closePageBlock();
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (IBAction)shareToFriend:(id)sender {
    if ([WXApi isWXAppInstalled]) {
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadImageWithURL:[NSURL URLWithString:APP_CONTEXT.account.data.avatar] options:SDWebImageHighPriority progress:NULL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
            WXWebpageObject *ext = [WXWebpageObject object];
            ext.webpageUrl = APP_CONTEXT.account.data.invitation_code_share_url;
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = APP_CONTEXT.account.data.invitation_code_share_title;
            message.description = APP_CONTEXT.account.data.invitation_code_share_content;
            message.mediaObject = ext;
            [message setThumbImage:[image imageCompressForWidth:45.f]];
            req.bText = NO;
            req.message = message;
            req.scene = WXSceneSession;
            [WXApi sendReq:req];
        }];
    } else {
        
    }
}

- (IBAction)shareToFriends:(id)sender {
    if ([WXApi isWXAppInstalled]) {
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadImageWithURL:[NSURL URLWithString:APP_CONTEXT.account.data.avatar] options:SDWebImageHighPriority progress:NULL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
            WXWebpageObject *ext = [WXWebpageObject object];
            ext.webpageUrl = APP_CONTEXT.account.data.invitation_code_share_url;
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = APP_CONTEXT.account.data.invitation_code_share_title;
            message.description = APP_CONTEXT.account.data.invitation_code_share_content;
            message.mediaObject = ext;
            [message setThumbImage:[image imageCompressForWidth:45.f]];
            req.bText = NO;
            req.message = message;
            req.scene = WXSceneTimeline;
            [WXApi sendReq:req];
        }];
    } else {
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
